<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :http-request="upload"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <!-- 进度条 -->
      <el-progress v-if="isShow" type="circle" :percentage="percentage" class="progress" />
    </el-upload>
  </div>
</template>

<script>
// 引入 COS 模块
const COS = require('cos-js-sdk-v5')
// 实例化对象
const cos = new COS({
  SecretId: 'AKIDeBUZSAfvuhHbibO1RglcDaliPtBF4sY2', // 身份识别 ID
  SecretKey: 'IL9k6DHBuOYZNyLYYKqAICtPiojGgvxn' // 身份秘钥
})
export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imageUrl: '',
      isShow: false,
      percentage: 0
    }
  },
  watch: {
    value() {
      this.imageUrl = this.value
    }
  },
  methods: {
    upload(val) {
      console.log(val)
      if (val.file) {
        this.isShow = true
        // 上传到腾讯云
        cos.putObject({
          Bucket: 'rr-1314536475', /* 填入您自己的存储桶，必须字段 */
          Region: 'ap-shanghai', /* 存储桶所在地域，例如 ap-beijing，必须字段 */
          Key: val.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: val.file, // 上传文件对象
          onProgress: (progressData) => { // 上传进度
            console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功之后
          if (data.statusCode === 200) {
            // data.Location 就是返回的图片地址
            this.imageUrl = `https:${data.Location}`
            this.$emit('input', this.imageUrl)
            this.isShow = false
          }
        })
      }
    },
    beforeAvatarUpload(file) {
      const typeList = [
        'image/jpeg',
        'image/jpg',
        'image/png',
        'image/webp',
        'image/gif'
      ]
      const isType = typeList.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isType) {
        this.$message.error('上传头像图片只能是 JPG/jpeg/png/webp/gif 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isType && isLt2M
    }
  }
}
</script>

  <style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .progress {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background: #fff;
  }
  </style>

